<%@page import="org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException"%>
<%@page import="net.sf.json.JSONObject"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.io.*, javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.io.output.*"%>
<%@ include file="/classes/DBHelper.jsp"%>
<%@ include file="/classes/CommonHelper.class.jsp"%>
<%
	JSONObject result = new JSONObject();
	String[] fileType = new String[] { ".jpg", ".gif", ".png", ".jpeg" };
	File file;
	int maxFileSize = 10 * 1024 * 1024;
	int maxMemSize = 10 * 1024 * 1024;
	ServletContext context = pageContext.getServletContext();
	String filePath = request.getRealPath(File.separator);
	String tmpPath = filePath + "zyfw" + File.separator + "temp" + File.separator;
	filePath += "zyfw" + File.separator + "upload" + File.separator;
	File f = new File(filePath);
	if (!f.exists()) {
		f.mkdirs();
	}
	File f2 = new File(tmpPath);
	if (!f2.exists()) {
		f2.mkdirs();
	}
	try {
		String ecode = "utf-8";
		// 验证上传内容了类型
		String contentType = request.getContentType();//multipart/form-data
		if ((contentType.indexOf("multipart/form-data") >= 0)) {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			// 设置内存中存储文件的最大值
			factory.setSizeThreshold(maxMemSize);
			// 本地存储的数据大于 maxMemSize.
			factory.setRepository(new File(tmpPath));
			// 创建一个新的文件上传处理程序
			ServletFileUpload upload = new ServletFileUpload(factory);
			// 设置最大上传的文件大小
			upload.setSizeMax(maxFileSize);
			//         try {
			// 解析获取的文件
			List<FileItem> fileItems = upload.parseRequest(request);
			// 处理上传的文件
			Iterator<FileItem> i = fileItems.iterator();
			String name = null;
			String phoneNum = null;
			String title = null;
			String content = null;
			String vCode = null;

			String realName1 = null;
			String realName2 = null;
			while (i.hasNext()) {
				FileItem fi = i.next();
				if (!fi.isFormField()) {//上传的文件
					String fileName = fi.getName();
					String suffix = null;
					if (fileName.indexOf(".") != -1) {
						suffix = fileName.substring(fileName.lastIndexOf("."));
					}
					if (!Arrays.asList(fileType).contains(suffix)) {
						result.put("status", -1);
						result.put("msg", "不支持此类型的文件上传");
						out.write(result.toString());
						return;
					}
					if ("file1".equals(fi.getFieldName())) {
						realName1 = System.currentTimeMillis() + suffix;
						String filePath1 = filePath + realName1;
						fi.write(new File(filePath1));
						System.out.println("filePath1:" + filePath1);
					}
					if ("file2".equals(fi.getFieldName())) {
						realName2 = System.currentTimeMillis() + suffix;
						String filePath2 = filePath + realName2;
						fi.write(new File(filePath2));
						System.out.println("filePath2:" + filePath2);
					}
				} else {
					//FormField
					if ("name".equals(fi.getFieldName())) {
						name = fi.getString(ecode);
					}
					if ("phoneNum".equals(fi.getFieldName())) {
						phoneNum = fi.getString(ecode);
					}
					if ("title".equals(fi.getFieldName())) {
						title = fi.getString(ecode);
					}
					if ("content".equals(fi.getFieldName())) {
						content = fi.getString(ecode);
					}
					if ("vCode".equals(fi.getFieldName())) {
						vCode = fi.getString(ecode);
					}
					System.out.println(fi.getFieldName() + ":" + fi.getString(ecode));
				}
			}

			if (null == vCode) {
				result.put("status", -1);
				result.put("msg", "请输入验证码");
				out.println(result);
				return;
			}
			//判断验证码
			String sessionCode = (String) session.getAttribute("vCode");

			if (null == sessionCode) {
				result.put("status", -1);
				result.put("msg", "验证码已失效");
				out.println(result);
				return;
			}
			if (!sessionCode.toLowerCase().equals(vCode.toLowerCase())) {
				result.put("status", -1);
				session.removeAttribute(vCode);
				result.put("msg", "验证码错误，请刷新验证码重试");
				out.println(result);
				return;
			}

			if (nullOrEmpty(name)) {
				result.put("status", -1);
				result.put("msg", "请输入姓名");
				out.println(result);
				return;
			}
			if (nullOrEmpty(phoneNum)) {
				result.put("status", -1);
				result.put("msg", "请输入手机号码");
				out.println(result);
				return;
			}
			if (nullOrEmpty(title)) {
				result.put("status", -1);
				result.put("msg", "请输入需求标题");
				out.println(result);
				return;
			}
			if (nullOrEmpty(content)) {
				result.put("status", -1);
				result.put("msg", "请输入需求内容");
				out.println(result);
				return;
			}
			//转换时间

			String createTime = formatDate("yyyy-MM-dd HH:mm:ss", new Date());
			String zyfw_id = getGuid();
			String sql = "INSERT INTO APP_ZYFW (zyfw_id,name,phone_num,title,content,status,create_time,apply_ip,file1,file2) values (?,?,?,?,?,?,to_date(?,'YYYY-MM-DD HH24:MI:SS'),?,?,?)";
			int count = executeUpdate(sql, new Object[] { zyfw_id, name, phoneNum, title, content, "A",
					createTime, getIpAddr(request), realName1, realName2 });
			if (count >= 1) {
				result.put("status", 200);
				result.put("msg", "提交成功");
				out.println(result);
			} else {
				result.put("status", -1);
				result.put("msg", "提交失败，请重试");
				out.println(result);
			}
		}
	} catch (SizeLimitExceededException e) {
		result.put("status", -1);
		result.put("msg", "上传失败,请确认上传的文件大小不能超过" + maxFileSize + "5M");
		out.write(result.toString());
	} catch (Exception e) {
		result.put("status", -1);
		result.put("msg", "系统错误");
		out.write(result.toString());
	}
%>
